﻿

/*
 *=====================================================================
 * Name    : MBL
 * Author  : LCFC RD SS
 * Copyright (c) 2012 - 2019, Hefei LCFC Information Technology Co.Ltd.
 *=====================================================================
 */

using MBX;
using MK;
using System;
using System.IO;
using System.IO.Packaging;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Xps;
using System.Windows.Xps.Packaging;


namespace AutoEditUI
{
    public class XpsEx
    {

        public static void ExportXpsAndPdf(string path, FrameworkElement surface)
        {
            Uri uri = new Uri(path + ".xps", UriKind.Absolute);


            Transform transform = surface.LayoutTransform;
            surface.LayoutTransform = null;
            Size size = new Size(surface.Width, surface.Height);
            surface.Measure(size);
            surface.Arrange(new Rect(size));
            {
                Package package = Package.Open(uri.LocalPath, FileMode.Create);
                XpsDocument doc = new XpsDocument(package);
                XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
                writer.Write(surface);
                doc.Close();
                package.Close();
            }
            {

            }
            surface.LayoutTransform = transform;



            FixedDocument2Pdf(surface, path);

        }
        public static void FixedDocument2Pdf(FrameworkElement surface, string path)
        {
            MemoryStream ms = new MemoryStream();
            FileStream fs = new FileStream(path + ".xps", FileMode.Open, FileAccess.Read);
            //Read all bytes into an array from the specified file.
            int nBytes = (int)fs.Length;//计算流的长度
            byte[] byteArray = new byte[nBytes];//初始化用于MemoryStream的Buffer
            int nBytesRead = fs.Read(byteArray, 0, nBytes);//将File里的内容一次性的全部读到byteArray中去
            ms = new MemoryStream(byteArray);//初始化MemoryStream,并将Buffer指向FileStream的读取结果数组

            // Get XPS file bytes
            var bytes = ms.ToArray();
            ms.Dispose();

            // Print to PDF
            var outputFilePath = path + ".pdf";
            PdfFilePrinter.PrintXpsToPdf(bytes, outputFilePath, "Document Title");
        }

        public static bool GenerateXpsAndPdf(string path, Canvas canvas)
        {
            try
            {
                if (path == null)
                {
                    LogHelper.Log(" ExportToXps path == null");
                    return false;
                }
                ExportXpsAndPdf(path, canvas);
                LogHelper.Log(" ExportXpsAndPdf Finish " + path);
                return true;
            }
            catch (Exception eee)
            {
                LogHelper.Log(" GenerateXpsAndPdf Exception " + eee.ToString());
                return false;
            }
        }
    }
}
